// src: https://github.com/JorelAli/mdBook-pagetoc

// Un-active everything when you click it
Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el, i) {
    el.addEventHandler("click", function() {
        Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el, i) {
            el.classList.remove("active");
        });
        el.classList.add("active");
    });
});

// var updateFunction = function() {

//     var id;
//     var elements = document.getElementsByClassName("header");
//     Array.prototype.forEach.call(elements, function(el, i) {
//         if (window.pageYOffset >= el.offsetTop) {
//             id = el;
//         }
//     });

//     Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el, i) {
//         el.classList.remove("active");
//     });

//     Array.prototype.forEach.call(document.getElementsByClassName("pagetoc")[0].children, function(el, i) {
//         if (id.href.localeCompare(el.href) == 0) {
//             el.classList.add("active");
//         }
//     });
// };


var updateFunction = function() {
    var currentId = null;
    var headers = document.getElementsByClassName("header");
    
    // 找到当前滚动位置对应的header元素ID
    Array.prototype.forEach.call(headers, function(header) {
        if (window.pageYOffset >= header.offsetTop - 50) { // 添加偏移量以提高用户体验
            currentId = header.id;
        }
    });

    // 移除所有目录项的active类
    var tocItems = document.getElementsByClassName("pagetoc")[0]?.children;
    if (!tocItems) return; // 确保pagetoc元素存在
    
    Array.prototype.forEach.call(tocItems, function(item) {
        item.classList.remove("active");
    });

    // 如果找到匹配的header ID，为对应的目录项添加active类
    if (currentId) {
        Array.prototype.forEach.call(tocItems, function(item) {
            // 从目录项的href中提取目标ID（例如 href="#section1" 提取 "section1"）
            var targetId = item.getAttribute("href")?.substring(1);
            if (targetId === currentId) {
                item.classList.add("active");
            }
        });
    }
};

// Populate sidebar on load
window.addEventListener('load', function() {
    var pagetoc = document.getElementsByClassName("pagetoc")[0];
    var elements = document.getElementsByClassName("header");
    Array.prototype.forEach.call(elements, function(el, i) {
        var link = document.createElement("a");

        // Indent shows hierarchy
        var indent = "";
        switch (el.parentElement.tagName) {
            case "H2":
                indent = "20px";
                break;
            case "H3":
                indent = "40px";
                break;
            case "H4":
                indent = "60px";
                break;
            default:
                break;
        }

        link.appendChild(document.createTextNode(el.text));
        link.style.paddingLeft = indent;
        link.href = el.href;
        pagetoc.appendChild(link);
    });
    updateFunction.call();
});



// Handle active elements on scroll
window.addEventListener("scroll", updateFunction);
